﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace AIC_KQKT
{
    public partial class BaoCaoMau10 : MyFromBase
    {
        #region PageLoad
        protected void Page_Load(object sender, EventArgs e)
        {
            CheckPermission();
            if (!IsPostBack)
            {
                BindDataToControls();
                if (Request.QueryString["KyBaoCao"] != null)
                {
                    cboKyBaoCao.SelectedValue = Request.QueryString["KyBaoCao"];
                    cboKyBaoCao_SelectedIndexChanged(sender, e);
                }
            }
        }

        /// <summary>
        /// Thủ tục kiểm tra quyền sử dụng của người dùng có được quyền truy cập vào mục này hay không?
        /// </summary>
        private void CheckPermission()
        {
            Dictionary<string, int> Role = MyCommon.CheckPermission("BaoCaoMau10");
            if (Role == null || Role["Xem"] == 0 || ((Role["Xem"] == 1 && Role["Sua"] == 0)))
            {
                Response.Redirect("Index.aspx?p=0");
            }
        }

        /// <summary>
        /// Bind Data vào các contrls như Combobox, ListBox, textBox ...
        /// </summary>
        private void BindDataToControls()
        {
            //TODO
            if (Session["userid"] != null)
            {
                string sql = "Select DV.* " +
                             "From tblDonVi DV " +
                             "Inner Join tblUser U On U.DonViID=DV.ID " +
                             "Where U.ID=" + Session["userid"];
                DataTable dtDV = SqlHelper.ExecuteDataTable(CommandType.Text, sql);
                tdTenDonVi.InnerText = "(" + dtDV.Rows[0]["MaDV"] + ") " + dtDV.Rows[0]["TenDV"];
                sql = "Select ID,TenDM From tblDanhMuc Where MaDM='" + MyConst.DanhMuc.KyBaoCao.Ma + "' ";
                SqlDataReader reKyBaoCao = SqlHelper.ExecuteReader(CommandType.Text, sql);
                clsDataBind.CreateCombo("Tất cả", reKyBaoCao, cboKyBaoCao, 1, 0);
            }
        }

        protected void BindDataToTextField()
        {
            string sql = "SELECT * FROM dbo.tblBaoCaoMau10 WHERE KyBaoCaoID_DM = @KyBaoCaoID_DM AND DonViID = @DonViID AND IsCommon = 1";
            List<SqlParameter> lstParameters = new List<SqlParameter>();
            lstParameters.Add(SqlHelper.CreateParameterObject("@KyBaoCaoID_DM", SqlDbType.Int, ParameterDirection.Input, cboKyBaoCao.SelectedValue));
            lstParameters.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, MyCommon.GetDonViID(Session["userid"].ToString())));
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql, lstParameters);
            if (dt.Rows.Count != 0)
            {
                txtSoBaoCao.Text = dt.Rows[0]["CM_SoBaoCao"].ToString();
                txtTenBaoCao.Text = dt.Rows[0]["CM_TenBaoCao"].ToString();
                txtNguoiLapBaoCao.Text = dt.Rows[0]["CM_NguoiLapBaoCao"].ToString();
                txtNgayBaoCao.Text = dt.Rows[0]["CM_NgayBaoCao"].Equals(DBNull.Value) ? "" : Convert.ToDateTime(dt.Rows[0]["CM_NgayBaoCao"]).ToString("dd/MM/yyyy");
                txtThuTruong.Text = dt.Rows[0]["CM_ThuTruong"].ToString();
                txtFrom.Text = dt.Rows[0]["CM_TuNgay"].Equals(DBNull.Value) ? "" : dt.Rows[0]["CM_TuNgay"].ToString();
                txtTo.Text = dt.Rows[0]["CM_DenNgay"].Equals(DBNull.Value) ? "" : dt.Rows[0]["CM_DenNgay"].ToString();
                for (int i = 1; i < 23; i++)
                {
                    TextBox txtData = (TextBox)MyCommon.FindControlIterative(this, "TextBox" + i);
                    txtData.Text = dt.Rows[0]["F" + i].ToString();
                }
            }

        }

        /// <summary>
        /// Bind Data vào gridview
        /// </summary>
        private void BindDataToGrid()
        {
            List<SqlParameter> lstParams = new List<SqlParameter>();
            lstParams.Add(SqlHelper.CreateParameterObject("@KyBaoCaoID_DM", SqlDbType.Int, ParameterDirection.Input, cboKyBaoCao.SelectedValue));
            lstParams.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, Session["userdonvi"]));
            lstParams.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, Session["userid"]));
            string sql = "SELECT  M1.* ,A.STT,A.TenDM ,A.BoiDen FROM dbo.tblBaoCaoMau10 M1 " +
                "INNER JOIN dbo.tblDanhMucBaoCao A ON M1.TenBaoCaoID_DM = A.ID " +
                "WHERE KyBaoCaoID_DM = @KyBaoCaoID_DM AND DonViID = @DonViID AND A.LaToChuc = 0";
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql, lstParams);
            grvData.DataSource = dt;
            grvData.DataBind();
            sql = "SELECT  M1.* ,A.STT,A.TenDM ,A.BoiDen FROM dbo.tblBaoCaoMau10 M1 " +
                "INNER JOIN dbo.tblDanhMucBaoCao A ON M1.TenBaoCaoID_DM = A.ID " +
                "WHERE KyBaoCaoID_DM = @KyBaoCaoID_DM AND DonViID = @DonViID AND A.LaToChuc = 1";
            dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql, lstParams);
            grvData1.DataSource = dt;
            grvData1.DataBind();
            sql = "SELECT  M1.* ,A.STT,A.TenDM ,A.BoiDen FROM dbo.tblBaoCaoMau10 M1 " +
                "INNER JOIN dbo.tblDanhMucBaoCao A ON M1.TenBaoCaoID_DM = A.ID " +
                "WHERE KyBaoCaoID_DM = @KyBaoCaoID_DM AND DonViID = @DonViID AND A.LaToChuc = 2";
            dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql, lstParams);
            grvData2.DataSource = dt;
            grvData2.DataBind();
            BindDataToGrid3();
            btnLuu.Visible = true;
            VisibleGridView(true);
        }

        #endregion

        #region Events

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("Index.aspx");
        }

        protected void VisibleGridView(bool visible)
        {
            grvData.Visible = visible;
            grvData1.Visible = visible;
            grvData2.Visible = visible;
            grvData3.Visible = visible;
            grvData4.Visible = visible;
        }

        protected void cboKyBaoCao_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboKyBaoCao.SelectedIndex > 0)
            {
                btnLuu.Visible = false;
                VisibleGridView(false);
                BindDataToGrid();
                if (((DataTable)grvData.DataSource).Rows.Count == 0)
                {
                    SqlTransaction MyTran = SqlHelper.BeginTransaction();
                    try
                    {
                        List<SqlParameter> lstParams = new List<SqlParameter>();
                        lstParams.Add(SqlHelper.CreateParameterObject("@KyBaoCaoID_DM", SqlDbType.Int, ParameterDirection.Input, cboKyBaoCao.SelectedValue));
                        lstParams.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, Session["userdonvi"]));
                        lstParams.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, Session["userid"]));
                        string sql = "INSERT INTO dbo.tblBaoCaoMau10( KyBaoCaoID_DM ,DonViID ,TenBaoCaoID_DM ,F1 ,F2 ,F3 ,F4 ,F5 ,F6 ,F7 , " +
                                 "F8,F9,F10,F11,F12,F13,F14,F15,F16,F17,F18,F19,F20,F21,F22,F23,F24,F25,F26,F27,UserID) " +
                                 "SELECT @KyBaoCaoID_DM ,@DonViID ,A.ID,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,@UserID " +
                                 "FROM dbo.tblDanhMucBaoCao A WHERE A.MaDM = 100";
                        SqlHelper.ExecuteNonQuery(System.Data.CommandType.Text, sql, lstParams);
                        sql = "INSERT INTO dbo.tblBaoCaoMau10(KyBaoCaoID_DM,DonViID,IsCommon ,UserID) " +
                              "VALUES( @KyBaoCaoID_DM,@DonViID,1,@UserID)";
                        SqlHelper.ExecuteNonQuery(System.Data.CommandType.Text, sql, lstParams);
                        MyTran.Commit();
                    }
                    catch
                    {
                        MyTran.Rollback();
                    }
                    grvData.EditIndex = -1;
                    grvData1.EditIndex = -1;
                    grvData2.EditIndex = -1;
                    grvData3.EditIndex = -1;
                    BindDataToGrid();
                }
                else
                {
                    BindDataToTextField();
                    VisibleGridView(true);
                }
            }
            else
            {
                btnLuu.Visible = false;
                VisibleGridView(false);
                ResetField();
            }
        }

        private void ResetField()
        {
            txtSoBaoCao.Text = "";
            txtTenBaoCao.Text = "";
            txtFrom.Text = "";
            txtTo.Text = "";
            txtNguoiLapBaoCao.Text = "";
            txtNgayBaoCao.Text = "";
            txtThuTruong.Text = "";
        }

        protected void SaveCommonField()
        {
            List<SqlParameter> lstParameter = new List<SqlParameter>();

        }

        protected void btnLuu_Click(object sender, EventArgs e)
        {

            string sSQL = "Update tblBaoCaoMau10 Set F1=@F1,F2=@F2,F3=@F3,F4=@F4,F5=@F5,F6=@F6,F7=@F7,F8=@F8,F9=@F9,F10=@F10,F11=@F11,F12=@F12,F13=@F13,F14=@F14,F15=@F15,F16=@F16,F17=@F17,F18=@F18,F19=@F19,F20=@F20,F21=@F21,F22=@F22," +
                          "CM_SoBaoCao=@CM_SoBaoCao,CM_TenBaoCao = @CM_TenBaoCao,CM_TuNgay = @CM_TuNgay,CM_DenNgay = @CM_DenNgay,CM_ThuTruong = @CM_ThuTruong,CM_NguoiLapBaoCao = @CM_NguoiLapBaoCao,CM_NgayBaoCao = @CM_NgayBaoCao,UpdateDate=GETDATE() " +
                          "Where IsCommon=1 And DonViID=" + MyCommon.GetDonViID(Session["userid"].ToString()) + " And KyBaoCaoID_DM=" + cboKyBaoCao.SelectedValue;

            List<SqlParameter> lstParameter = new List<SqlParameter>();
            for (int i = 1; i < 23; i++)
            {
                TextBox txtData = (TextBox)MyCommon.FindControlIterative(this, "TextBox" + i);
                lstParameter.Add(SqlHelper.CreateParameterObject("@F" + i, SqlDbType.Int, ParameterDirection.Input, txtData.Text.Equals("") ? 0 : Convert.ToInt32(txtData.Text.Trim())));
            }

            lstParameter.Add(SqlHelper.CreateParameterObject("@CM_SoBaoCao", SqlDbType.NVarChar, ParameterDirection.Input, txtSoBaoCao.Text.Trim()));
            lstParameter.Add(SqlHelper.CreateParameterObject("@CM_TenBaoCao", SqlDbType.NVarChar, ParameterDirection.Input, txtTenBaoCao.Text.Trim()));
            lstParameter.Add(SqlHelper.CreateParameterObject("@CM_NguoiLapBaoCao", SqlDbType.NVarChar, ParameterDirection.Input, txtNguoiLapBaoCao.Text.Trim()));
            lstParameter.Add(SqlHelper.CreateParameterObject("@CM_NgayBaoCao", SqlDbType.NVarChar, ParameterDirection.Input, txtNgayBaoCao.Text.Trim()));
            lstParameter.Add(SqlHelper.CreateParameterObject("@CM_ThuTruong", SqlDbType.NVarChar, ParameterDirection.Input, txtThuTruong.Text.Trim()));
            lstParameter.Add(SqlHelper.CreateParameterObject("@CM_TuNgay", SqlDbType.NVarChar, ParameterDirection.Input, txtFrom.Text.Trim()));
            lstParameter.Add(SqlHelper.CreateParameterObject("@CM_DenNgay", SqlDbType.NVarChar, ParameterDirection.Input, txtTo.Text.Trim()));

            try
            {
                SqlHelper.ExecuteNonQuery(CommandType.Text, sSQL, lstParameter);
                MyCommon.AddMessage(this.Master, "Cập nhật báo cáo thành công !", MyConst.MessageType.Success);
            }
            catch (Exception)
            {
                MyCommon.AddMessage(this.Master, "Cập nhật báo cáo lỗi !", MyConst.MessageType.Error);
            }


        }

        private static void AddMergedCells(GridViewRow objgridviewrow, TableCell objtablecell, int colspan, int rowspan, string celltext)
        {
            objtablecell = new TableCell();
            objtablecell.Text = celltext;
            objtablecell.ColumnSpan = colspan;
            objtablecell.RowSpan = rowspan;
            objtablecell.HorizontalAlign = HorizontalAlign.Center;
            objgridviewrow.Cells.Add(objtablecell);
        }
        #endregion Events


        #region GridCommand

        protected void grvData_RowCreated(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.Header)
            {

                GridView objGridView = (GridView)sender;
                //GridViewRow objgridviewrow = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);

                GridViewRow objgridviewrow1 = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
                GridViewRow objgridviewrow2 = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
                GridViewRow objgridviewrow3 = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
                GridViewRow objgridviewrow4 = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
                GridViewRow objgridviewrow5 = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);

                TableCell objtablecell = new TableCell();
                AddMergedCells(objgridviewrow1, objtablecell, 2, 4, "");
                AddMergedCells(objgridviewrow1, objtablecell, 1, 4, "Tổng số UBKT");
                AddMergedCells(objgridviewrow1, objtablecell, 1, 4, "Tổng số ủy viên");
                AddMergedCells(objgridviewrow1, objtablecell, 4, 1, "CHUYÊN TRÁCH");
                AddMergedCells(objgridviewrow1, objtablecell, 1, 1, "");
                AddMergedCells(objgridviewrow1, objtablecell, 11, 1, "ỦY VIÊN KIÊM CHỨC CẢ CHỦ NHIỆM");
                AddMergedCells(objgridviewrow1, objtablecell, 5, 2, "RIÊNG CHỦ NHIỆM");
                AddMergedCells(objgridviewrow1, objtablecell, 1, 4, "Sửa");
                objgridviewrow1.BackColor = System.Drawing.Color.FromArgb(0x10, 0x9A, 0xEA);
                objgridviewrow1.ForeColor = System.Drawing.Color.White;
                objgridviewrow1.Font.Bold = true;
                objGridView.Controls[0].Controls.AddAt(0, objgridviewrow1);

                objtablecell = new TableCell();
                AddMergedCells(objgridviewrow2, objtablecell, 1, 3, "Số lượng");
                AddMergedCells(objgridviewrow2, objtablecell, 2, 1, "PHÓ CN");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 3, "Ủy viên chuyên trách");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 3, "Số lượng");
                AddMergedCells(objgridviewrow2, objtablecell, 2, 1, "Chỉ huy TM");
                AddMergedCells(objgridviewrow2, objtablecell, 2, 1, "Hậu cần");
                AddMergedCells(objgridviewrow2, objtablecell, 2, 1, "Kỹ thuật");
                AddMergedCells(objgridviewrow2, objtablecell, 4, 1, "Cán bộ chính trị");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 3, "Chuyên môn khác");
                objgridviewrow2.BackColor = System.Drawing.Color.FromArgb(0x10, 0x9A, 0xEA);
                objgridviewrow2.ForeColor = System.Drawing.Color.White;
                objgridviewrow2.Font.Bold = true;
                objGridView.Controls[0].Controls.AddAt(1, objgridviewrow2);

                objtablecell = new TableCell();
                AddMergedCells(objgridviewrow3, objtablecell, 1, 2, "Số lượng");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 2, "Là cấp UV cùng cấp");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 2, "Số lượng");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 2, "Là cấp UV cùng cấp");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 2, "Số lượng");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 2, "Là cấp UV cùng cấp");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 2, "Số lượng");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 2, "Là cấp UV cùng cấp");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 2, "Số lượng");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 2, "Tổ chức");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 2, "Cán bộ");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 2, "Loại khác");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 2, "Số lượng");
                AddMergedCells(objgridviewrow3, objtablecell, 4, 1, "Trong đó cấp ủy,chỉ huy cùng cấp");

                objgridviewrow3.BackColor = System.Drawing.Color.FromArgb(0x10, 0x9A, 0xEA);
                objgridviewrow3.ForeColor = System.Drawing.Color.White;
                objgridviewrow3.Font.Bold = true;
                objGridView.Controls[0].Controls.AddAt(2, objgridviewrow3);

                objtablecell = new TableCell();
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "BT,PBT");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "U.V T.Vụ");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "Chính ủy");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "Phó chính ủy");
                objgridviewrow4.BackColor = System.Drawing.Color.FromArgb(0x10, 0x9A, 0xEA);
                objgridviewrow4.ForeColor = System.Drawing.Color.White;
                objgridviewrow4.Font.Bold = true;
                objGridView.Controls[0].Controls.AddAt(3, objgridviewrow4);

                objtablecell = new TableCell();
                AddMergedCells(objgridviewrow5, objtablecell, 1, 1, "A");
                AddMergedCells(objgridviewrow5, objtablecell, 1, 1, "B");
                AddMergedCells(objgridviewrow5, objtablecell, 1, 1, "1");
                AddMergedCells(objgridviewrow5, objtablecell, 1, 1, "2");
                AddMergedCells(objgridviewrow5, objtablecell, 1, 1, "3");
                AddMergedCells(objgridviewrow5, objtablecell, 1, 1, "4");
                AddMergedCells(objgridviewrow5, objtablecell, 1, 1, "5");
                AddMergedCells(objgridviewrow5, objtablecell, 1, 1, "6");
                AddMergedCells(objgridviewrow5, objtablecell, 1, 1, "7");
                AddMergedCells(objgridviewrow5, objtablecell, 1, 1, "8");
                AddMergedCells(objgridviewrow5, objtablecell, 1, 1, "9");
                AddMergedCells(objgridviewrow5, objtablecell, 1, 1, "10");
                AddMergedCells(objgridviewrow5, objtablecell, 1, 1, "11");
                AddMergedCells(objgridviewrow5, objtablecell, 1, 1, "12");
                AddMergedCells(objgridviewrow5, objtablecell, 1, 1, "13");
                AddMergedCells(objgridviewrow5, objtablecell, 1, 1, "14");
                AddMergedCells(objgridviewrow5, objtablecell, 1, 1, "15");
                AddMergedCells(objgridviewrow5, objtablecell, 1, 1, "16");
                AddMergedCells(objgridviewrow5, objtablecell, 1, 1, "17");
                AddMergedCells(objgridviewrow5, objtablecell, 1, 1, "18");
                AddMergedCells(objgridviewrow5, objtablecell, 1, 1, "19");
                AddMergedCells(objgridviewrow5, objtablecell, 1, 1, "20");
                AddMergedCells(objgridviewrow5, objtablecell, 1, 1, "21");
                AddMergedCells(objgridviewrow5, objtablecell, 1, 1, "22");
                AddMergedCells(objgridviewrow5, objtablecell, 1, 1, "23");

                objgridviewrow5.BackColor = System.Drawing.Color.FromArgb(0xC7, 0xF4, 0x9C);
                objGridView.Controls[0].Controls.AddAt(4, objgridviewrow5);
            }
        }

        protected void grvData_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView dtr = (DataRowView)e.Row.DataItem;
                string[] BoiDen = dtr.Row.ItemArray[44].ToString().Split(',');
                if (BoiDen.Length >= 1)
                {
                    if (!BoiDen[0].Equals(""))
                    {
                        for (int i = 0; i < BoiDen.Length; i++)
                        {

                            e.Row.Cells[Convert.ToInt32(BoiDen[i]) + 1].BackColor = System.Drawing.Color.LightGray;
                            e.Row.Cells[Convert.ToInt32(BoiDen[i]) + 1].Enabled = false;
                        }
                    }
                }
            }
            RowDataBound(sender, e);
        }

        protected void grvData_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string sID = e.CommandArgument.ToString().Trim();
            string sCommandName = e.CommandName;

            if ((sCommandName == AIC_KQKT.MyConst.Action.Approve))
            {
                List<SqlParameter> arrParams = new List<SqlParameter>();
                for (int i = 2; i <= 24; i++)
                {
                    if (i != 3 && i != 4 && i != 8 && i != 15 && i != 20)
                    {
                        TextBox txtField = (TextBox)grvData.Rows[grvData.EditIndex].Cells[i].Controls[0];
                        arrParams.Add(SqlHelper.CreateParameterObject("F" + (i - 1), SqlDbType.Float, ParameterDirection.Input, txtField.Text));
                    }
                }
                string sql = "UPDATE tblBaoCaoMau10 SET [F1]=@F1,[F2]=@F4+@F6+@F8+@F10+@F12+@F15+@F16+@F17+@F18,[F3]=@F4+@F6," +
                             "[F4]=@F4,[F5]=@F5,[F6]=@F6,[F7]=@F8+@F10+@F12+@F15+@F16+@F17+@F18,[F8]=@F8,[F9]=@F9,[F10]=@F10,[F11]=@F11," +
                             "[F12]=@F12,[F13]=@F13,[F14]=@F15+@F16+@F17,[F15]=@F15,[F16]=@F16,[F17]=@F17,[F18]=@F18," +
                             "[F19]=@F20+@F21,[F20]=@F20,[F21]=@F21,[F22]=@F22," +
                             "[F23]=@F23,[UpdateDate]=GETDATE(),[UserID]=@UserID WHERE ID=@ID ";
                arrParams.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, Session["userid"]));
                arrParams.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, Convert.ToInt32(sID)));

                SqlHelper.ExecuteNonQuery(CommandType.Text, sql, arrParams);
                MyCommon.AddMessage(this.Master, "Cập nhật báo cáo thành công !", MyConst.MessageType.Success);
                grvData.EditIndex = -1;
                BindDataToGrid();
            }
        }

        protected void grvData_RowEditing(object sender, GridViewEditEventArgs e)
        {

            grvData.EditIndex = e.NewEditIndex;
            BindDataToGrid();
        }

        protected void grvData_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grvData.EditIndex = -1;
            BindDataToGrid();
        }

        #endregion GridCommand

        #region Grid1Command

        protected void grvData1_RowCreated(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.Header)
            {

                GridView objGridView = (GridView)sender;
                //GridViewRow objgridviewrow = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);

                GridViewRow objgridviewrow = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
                GridViewRow objgridviewrow2 = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
                GridViewRow objgridviewrow4 = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);

                TableCell objtablecell = new TableCell();
                AddMergedCells(objgridviewrow, objtablecell, 2, 2, "");
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "Tổng số theo biên chế");
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "Tổng số hiện có");
                AddMergedCells(objgridviewrow, objtablecell, 3, 1, "TRONG ĐÓ");
                AddMergedCells(objgridviewrow, objtablecell, 4, 1, "TRÌNH ĐỘ LÝ LUẬN CHÍNH TRỊ");
                AddMergedCells(objgridviewrow, objtablecell, 3, 1, "TRÌNH ĐỘ VĂN HÓA");
                AddMergedCells(objgridviewrow, objtablecell, 4, 1, "THỜI GIAN CÔNG TÁC TRONG NGHÀNH");
                AddMergedCells(objgridviewrow, objtablecell, 4, 1, "TUỔI ĐỜI");
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "UV UBKT và cán bộ KT ở từng cấp bị XLKL");
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "Sửa");
                objgridviewrow.BackColor = System.Drawing.Color.FromArgb(0x10, 0x9A, 0xEA);
                objgridviewrow.ForeColor = System.Drawing.Color.White;
                objgridviewrow.Font.Bold = true;
                objGridView.Controls[0].Controls.AddAt(0, objgridviewrow);

                objtablecell = new TableCell();
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "PCN T.trực");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "UV C.trách");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Cán bộ KT");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Cao cấp");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Trung cấp");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Sơ cấp");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Cơ sở");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Đh trên ĐH");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Phổ thông TH");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Phổ thông cơ sở");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Dưới 2 năm");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Từ 2 đến 5 năm");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Từ 6 đến 10 năm");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Trên 10 năm");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Dưới 30 tuổi");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Từ 30 đến 40 tuổi");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Từ 41 đến 50 tuổi");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Trên 50 tuổi");
                objgridviewrow2.BackColor = System.Drawing.Color.FromArgb(0x10, 0x9A, 0xEA);
                objgridviewrow2.ForeColor = System.Drawing.Color.White;
                objgridviewrow2.Font.Bold = true;
                objGridView.Controls[0].Controls.AddAt(1, objgridviewrow2);

                objtablecell = new TableCell();
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "A");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "B");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "1");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "2");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "3");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "4");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "5");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "6");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "7");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "8");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "9");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "10");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "11");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "12");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "13");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "14");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "15");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "16");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "17");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "18");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "19");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "20");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "21");

                objgridviewrow4.BackColor = System.Drawing.Color.FromArgb(0xC7, 0xF4, 0x9C);
                objGridView.Controls[0].Controls.AddAt(2, objgridviewrow4);
            }
        }

        protected void grvData1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView dtr = (DataRowView)e.Row.DataItem;
                string[] BoiDen = dtr.Row.ItemArray[44].ToString().Split(',');
                if (BoiDen.Length >= 1)
                {
                    if (!BoiDen[0].Equals(""))
                    {
                        for (int i = 0; i < BoiDen.Length; i++)
                        {

                            e.Row.Cells[Convert.ToInt32(BoiDen[i]) + 1].BackColor = System.Drawing.Color.LightGray;
                            e.Row.Cells[Convert.ToInt32(BoiDen[i]) + 1].Enabled = false;
                        }
                    }
                }
            }
            RowDataBound(sender, e);
        }

        protected void grvData1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string sID = e.CommandArgument.ToString().Trim();
            string sCommandName = e.CommandName;

            if ((sCommandName == AIC_KQKT.MyConst.Action.Approve))
            {
                List<SqlParameter> arrParams = new List<SqlParameter>();
                for (int i = 2; i <= 22; i++)
                {
                    if (i != 3)
                    {
                        TextBox txtField = (TextBox)grvData1.Rows[grvData1.EditIndex].Cells[i].Controls[0];
                        arrParams.Add(SqlHelper.CreateParameterObject("F" + (i - 1), SqlDbType.Float, ParameterDirection.Input, txtField.Text));
                    }
                }
                string sql = "UPDATE tblBaoCaoMau10 SET [F1]=@F1,[F2]=@F3+@F4+@F5,[F3]=@F3," +
                             "[F4]=@F4,[F5]=@F5,[F6]=@F6,[F7]=@F7,[F8]=@F8,[F9]=@F9,[F10]=@F10,[F11]=@F11," +
                             "[F12]=@F12,[F13]=@F13,[F14]=@F14,[F15]=@F15,[F16]=@F16,[F17]=@F17,[F18]=@F18," +
                             "[F19]=@F19,[F20]=@F20,[F21]=@F21," +
                             "[UpdateDate]=GETDATE(),[UserID]=@UserID WHERE ID=@ID ";
                arrParams.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, Session["userid"]));
                arrParams.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, Convert.ToInt32(sID)));
                //C2 = C3 + C4 + C5
                int C2 = Convert.ToInt32(arrParams[1].Value) +
                                Convert.ToInt32(arrParams[2].Value) +
                                Convert.ToInt32(arrParams[3].Value);
                //C2 = C6 + C7 + C8 + C9
                int C2Compare1 = Convert.ToInt32(arrParams[4].Value) +
                                Convert.ToInt32(arrParams[5].Value) +
                                Convert.ToInt32(arrParams[6].Value) +
                                Convert.ToInt32(arrParams[7].Value);
                //C2 = C10 + C11 + C12
                int C2Compare2 = Convert.ToInt32(arrParams[8].Value) +
                                Convert.ToInt32(arrParams[9].Value) +
                                Convert.ToInt32(arrParams[10].Value);
                //C2 = C13 + C14 + C15 + C16
                int C2Compare3 = Convert.ToInt32(arrParams[11].Value) +
                                Convert.ToInt32(arrParams[12].Value) +
                                Convert.ToInt32(arrParams[13].Value) +
                                Convert.ToInt32(arrParams[14].Value);
                //C2 = C17 + C18 + C19 + C20
                int C2Compare4 = Convert.ToInt32(arrParams[15].Value) +
                                Convert.ToInt32(arrParams[16].Value) +
                                Convert.ToInt32(arrParams[17].Value) +
                                Convert.ToInt32(arrParams[18].Value);
                if (C2 != C2Compare1)
                {
                    string mess = "Có sai sót trong dữ liệu nhập vào, chú ý ! <br/>" +
                                  "C2 = C6 + C7 + C8 + C9" +
                                  "<br /> Bạn vừa nhập C2 = <font color=red>" + C2 + "</font> và C6 + C7 + C8 + C9 = <font color=red>" + C2Compare1 + "</font> -> Sai quy tắc nhập dữ liệu !";
                    MyCommon.AddMessage(this.Master, mess, MyConst.MessageType.Warning);
                }
                else if (C2 != C2Compare2)
                {
                    string mess = "Có sai sót trong dữ liệu nhập vào, chú ý ! <br/>" +
                                  "C2 = C10 + C11 + C12" +
                                  "<br /> Bạn vừa nhập C2 = <font color=red>" + C2 + "</font> và C10 + C11 + C12 = <font color=red>" + C2Compare2 + "</font> -> Sai quy tắc nhập dữ liệu !";
                    MyCommon.AddMessage(this.Master, mess, MyConst.MessageType.Warning);
                }
                else if (C2 != C2Compare3)
                {
                    string mess = "Có sai sót trong dữ liệu nhập vào, chú ý ! <br/>" +
                                  "C2 = C13 + C14 + C15 + C16" +
                                  "<br /> Bạn vừa nhập C2 = <font color=red>" + C2 + "</font> và C13 + C14 + C15 + C16 = <font color=red>" + C2Compare3 + "</font> -> Sai quy tắc nhập dữ liệu !";
                    MyCommon.AddMessage(this.Master, mess, MyConst.MessageType.Warning);
                }
                else if (C2 != C2Compare4)
                {
                    string mess = "Có sai sót trong dữ liệu nhập vào, chú ý ! <br/>" +
                                  "C2 = C17 + C18 + C19 + C20" +
                                  "<br /> Bạn vừa nhập C2 = <font color=red>" + C2 + "</font> và C17 + C18 + C19 + C20 = <font color=red>" + C2Compare4 + "</font> -> Sai quy tắc nhập dữ liệu !";
                    MyCommon.AddMessage(this.Master, mess, MyConst.MessageType.Warning);
                }
                else
                {
                    SqlHelper.ExecuteNonQuery(CommandType.Text, sql, arrParams);
                    MyCommon.AddMessage(this.Master, "Cập nhật báo cáo thành công !", MyConst.MessageType.Success);
                    grvData1.EditIndex = -1;
                    BindDataToGrid();
                }
            }
        }

        protected void grvData1_RowEditing(object sender, GridViewEditEventArgs e)
        {

            grvData1.EditIndex = e.NewEditIndex;
            BindDataToGrid();
        }

        protected void grvData1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grvData1.EditIndex = -1;
            BindDataToGrid();
        }

        #endregion Grid1Command

        #region Grid2Command

        protected void grvData2_RowCreated(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.Header)
            {

                GridView objGridView = (GridView)sender;
                //GridViewRow objgridviewrow = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);

                GridViewRow objgridviewrow = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
                GridViewRow objgridviewrow2 = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
                GridViewRow objgridviewrow3 = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
                GridViewRow objgridviewrow4 = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);

                TableCell objtablecell = new TableCell();
                AddMergedCells(objgridviewrow, objtablecell, 2, 3, "");
                AddMergedCells(objgridviewrow, objtablecell, 7, 1, "BỒI DƯỠNG NGHIỆP VỤ CÔNG TÁC KIỂM TRA");
                AddMergedCells(objgridviewrow, objtablecell, 16, 1, "LÝ LUẬN CHÍNH TRỊ");
                AddMergedCells(objgridviewrow, objtablecell, 4, 1, "CÁC CHUYÊN NGHÀNH KHÁC");
                AddMergedCells(objgridviewrow, objtablecell, 1, 3, "Sửa");
                objgridviewrow.BackColor = System.Drawing.Color.FromArgb(0x10, 0x9A, 0xEA);
                objgridviewrow.ForeColor = System.Drawing.Color.White;
                objgridviewrow.Font.Bold = true;
                objGridView.Controls[0].Controls.AddAt(0, objgridviewrow);

                objtablecell = new TableCell();
                AddMergedCells(objgridviewrow2, objtablecell, 1, 2, "Tổng số lớp đã mở");
                AddMergedCells(objgridviewrow2, objtablecell, 6, 1, "SỐ NGƯỜI ĐƯỢC BỒI DƯỠNG");
                AddMergedCells(objgridviewrow2, objtablecell, 4, 1, "CAO CẤP");
                AddMergedCells(objgridviewrow2, objtablecell, 4, 1, "TRUNG CẤP");
                AddMergedCells(objgridviewrow2, objtablecell, 4, 1, "SƠ CẤP");
                AddMergedCells(objgridviewrow2, objtablecell, 4, 1, "CƠ SỞ");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 2, "Tổng số");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 2, "CN, PCN");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 2, "Ủy viên");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 2, "Cán bộ KT");
                objgridviewrow2.BackColor = System.Drawing.Color.FromArgb(0x10, 0x9A, 0xEA);
                objgridviewrow2.ForeColor = System.Drawing.Color.White;
                objgridviewrow2.Font.Bold = true;
                objGridView.Controls[0].Controls.AddAt(1, objgridviewrow2);

                objtablecell = new TableCell();
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Tổng số");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "CN,PCN UBKT");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Ủy viên UBKT");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Cán bộ kiểm tra");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Cấp ủy viên");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Đảng viên");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Tổng số");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "CN,PCN");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Ủy viên");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Cán bộ KT");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Tổng số");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "CN,PCN");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Ủy viên");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Cán bộ KT");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Tổng số");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "CN,PCN");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Ủy viên");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Cán bộ KT");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Tổng số");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "CN,PCN");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Ủy viên");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Cán bộ KT");
                objgridviewrow3.BackColor = System.Drawing.Color.FromArgb(0x10, 0x9A, 0xEA);
                objgridviewrow3.ForeColor = System.Drawing.Color.White;
                objgridviewrow3.Font.Bold = true;
                objGridView.Controls[0].Controls.AddAt(2, objgridviewrow3);

                objtablecell = new TableCell();
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "A");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "B");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "1");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "2");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "3");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "4");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "5");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "6");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "7");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "8");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "9");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "10");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "11");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "12");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "13");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "14");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "15");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "16");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "17");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "18");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "19");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "20");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "21");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "22");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "23");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "24");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "25");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "26");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "27");

                objgridviewrow4.BackColor = System.Drawing.Color.FromArgb(0xC7, 0xF4, 0x9C);
                objGridView.Controls[0].Controls.AddAt(3, objgridviewrow4);
            }
        }

        protected void grvData2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView dtr = (DataRowView)e.Row.DataItem;
                string[] BoiDen = dtr.Row.ItemArray[44].ToString().Split(',');
                if (BoiDen.Length >= 1)
                {
                    if (!BoiDen[0].Equals(""))
                    {
                        for (int i = 0; i < BoiDen.Length; i++)
                        {

                            e.Row.Cells[Convert.ToInt32(BoiDen[i]) + 1].BackColor = System.Drawing.Color.LightGray;
                            e.Row.Cells[Convert.ToInt32(BoiDen[i]) + 1].Enabled = false;
                        }
                    }
                }
            }
            RowDataBound(sender, e);
        }

        protected void grvData2_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string sID = e.CommandArgument.ToString().Trim();
            string sCommandName = e.CommandName;

            if ((sCommandName == AIC_KQKT.MyConst.Action.Approve))
            {
                List<SqlParameter> arrParams = new List<SqlParameter>();
                for (int i = 2; i <= 28; i++)
                {
                    if (i != 3 && i != 9 && i != 13 && i != 17 && i != 21 && i != 25)
                    {
                        TextBox txtField = (TextBox)grvData2.Rows[grvData2.EditIndex].Cells[i].Controls[0];
                        arrParams.Add(SqlHelper.CreateParameterObject("F" + (i - 1), SqlDbType.Float, ParameterDirection.Input, txtField.Text));
                    }
                }
                string sql = "UPDATE tblBaoCaoMau10 SET [F1]=@F1,[F2]=@F3+@F4+@F5+@F6+@F7,[F3]=@F3," +
                             "[F4]=@F4,[F5]=@F5,[F6]=@F6,[F7]=@F7,[F8]=@F9+@F10+@F11,[F9]=@F9,[F10]=@F10,[F11]=@F11," +
                             "[F12]=@F13+@F14+@F15,[F13]=@F13,[F14]=@F14,[F15]=@F15,[F16]=@F17+@F18+@F19,[F17]=@F17,[F18]=@F18," +
                             "[F19]=@F19,[F20]=@F21+@F22+@F23,[F21]=@F21,[F22]=@F22," +
                             "[F23]=@F23,[F24]=@F25+@F26+@F27,[F25]=@F25,[F26]=@F26,[F27]=@F27," +
                             "[UpdateDate]=GETDATE(),[UserID]=@UserID WHERE ID=@ID ";
                arrParams.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, Session["userid"]));
                arrParams.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, Convert.ToInt32(sID)));

                SqlHelper.ExecuteNonQuery(CommandType.Text, sql, arrParams);
                MyCommon.AddMessage(this.Master, "Cập nhật báo cáo thành công !", MyConst.MessageType.Success);
                grvData2.EditIndex = -1;
                BindDataToGrid();
            }
        }

        protected void grvData2_RowEditing(object sender, GridViewEditEventArgs e)
        {

            grvData2.EditIndex = e.NewEditIndex;
            BindDataToGrid();
        }

        protected void grvData2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grvData2.EditIndex = -1;
            BindDataToGrid();
        }

        #endregion Grid2Command

        #region Grid3Command

        protected void grvData3_RowCreated(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.Header)
            {

                GridView objGridView = (GridView)sender;
                //GridViewRow objgridviewrow = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);

                GridViewRow objgridviewrow = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
                GridViewRow objgridviewrow2 = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
                GridViewRow objgridviewrow3 = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);

                TableCell objtablecell = new TableCell();
                AddMergedCells(objgridviewrow, objtablecell, 1, 3, "SỐ TT");
                AddMergedCells(objgridviewrow, objtablecell, 1, 3, "HỌ VÀ TÊN");
                AddMergedCells(objgridviewrow, objtablecell, 1, 3, "Năm sinh");
                AddMergedCells(objgridviewrow, objtablecell, 1, 3, "Đơn vị");
                AddMergedCells(objgridviewrow, objtablecell, 1, 3, "Cấp bấc");
                AddMergedCells(objgridviewrow, objtablecell, 3, 1, "CHỨC VỤ");
                AddMergedCells(objgridviewrow, objtablecell, 1, 3, "Văn hóa");
                AddMergedCells(objgridviewrow, objtablecell, 6, 1, "ĐÃ QUA TRƯỜNG LỚP");
                AddMergedCells(objgridviewrow, objtablecell, 1, 3, "QUÊ QUÁN");
                AddMergedCells(objgridviewrow, objtablecell, 1, 3, "Sửa");
                objgridviewrow.BackColor = System.Drawing.Color.FromArgb(0x10, 0x9A, 0xEA);
                objgridviewrow.ForeColor = System.Drawing.Color.White;
                objgridviewrow.Font.Bold = true;
                objGridView.Controls[0].Controls.AddAt(0, objgridviewrow);

                objtablecell = new TableCell();
                AddMergedCells(objgridviewrow2, objtablecell, 1, 2, "Đảng");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 2, "Chính quyền");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 2, "Trong ủy ban kiểm tra");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 2, "Học viện");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 2, "Trường sĩ quan");
                AddMergedCells(objgridviewrow2, objtablecell, 3, 1, "Bồi dưỡng nghiệp vụ về kiểm tra");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 2, "Thời gian công tác trong nghành");
                objgridviewrow2.BackColor = System.Drawing.Color.FromArgb(0x10, 0x9A, 0xEA);
                objgridviewrow2.ForeColor = System.Drawing.Color.White;
                objgridviewrow2.Font.Bold = true;
                objGridView.Controls[0].Controls.AddAt(1, objgridviewrow2);

                objtablecell = new TableCell();
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "UBKT TƯ mở");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "UBKT QUTW mở");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Cấp T.thuộc QUTW mở");
                objgridviewrow3.BackColor = System.Drawing.Color.FromArgb(0x10, 0x9A, 0xEA);
                objgridviewrow3.ForeColor = System.Drawing.Color.White;
                objgridviewrow3.Font.Bold = true;
                objGridView.Controls[0].Controls.AddAt(2, objgridviewrow3);
            }
        }

        protected void grvData3_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            RowDataBound(sender, e);
        }

        protected void grvData3_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string sID = e.CommandArgument.ToString().Trim();
            string sCommandName = e.CommandName;


            if ((sCommandName == AIC_KQKT.MyConst.Action.Approve))
            {
                SqlTransaction MyTrans = SqlHelper.BeginTransaction();
                try
                {

                    // cap nhat vao database
                    TextBox txtF1 = (TextBox)grvData3.Rows[grvData3.EditIndex].FindControl("txtF1");
                    TextBox txtF2 = (TextBox)grvData3.Rows[grvData3.EditIndex].FindControl("txtF2");
                    TextBox txtF3 = (TextBox)grvData3.Rows[grvData3.EditIndex].FindControl("txtF3");
                    TextBox txtF4 = (TextBox)grvData3.Rows[grvData3.EditIndex].FindControl("txtF4");
                    TextBox txtF5 = (TextBox)grvData3.Rows[grvData3.EditIndex].FindControl("txtF5");
                    TextBox txtF6 = (TextBox)grvData3.Rows[grvData3.EditIndex].FindControl("txtF6");
                    TextBox txtF7 = (TextBox)grvData3.Rows[grvData3.EditIndex].FindControl("txtF7");
                    TextBox txtF8 = (TextBox)grvData3.Rows[grvData3.EditIndex].FindControl("txtF8");
                    TextBox txtF9 = (TextBox)grvData3.Rows[grvData3.EditIndex].FindControl("txtF9");
                    TextBox txtF10 = (TextBox)grvData3.Rows[grvData3.EditIndex].FindControl("txtF10");
                    TextBox txtF11 = (TextBox)grvData3.Rows[grvData3.EditIndex].FindControl("txtF11");
                    TextBox txtF12 = (TextBox)grvData3.Rows[grvData3.EditIndex].FindControl("txtF12");
                    TextBox txtF13 = (TextBox)grvData3.Rows[grvData3.EditIndex].FindControl("txtF13");
                    TextBox txtF14 = (TextBox)grvData3.Rows[grvData3.EditIndex].FindControl("txtF14");
                    TextBox txtF15 = (TextBox)grvData3.Rows[grvData3.EditIndex].FindControl("txtF15");

                    string sSQL = "UPDATE dbo.tblBaoCaoMau10_B " +
                                  "SET F1=@F1,F2=@F2,F3=@F3,F4=@F4,F5=@F5,F6=@F6,F7=@F7,F8=@F8,F9=@F9,F10=@F10,F11=@F11,F12=@F12,F13=@F13,F14=@F14,F15=@F15" +
                                  ",UpdateDate = GETDATE(),UserID = @UserID WHERE ID = @ID";

                    List<SqlParameter> lstParameter = new List<SqlParameter>();

                    lstParameter.Add(SqlHelper.CreateParameterObject("@F1", SqlDbType.NVarChar, ParameterDirection.Input, txtF1.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F2", SqlDbType.NVarChar, ParameterDirection.Input, txtF2.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F3", SqlDbType.NVarChar, ParameterDirection.Input, txtF3.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F4", SqlDbType.NVarChar, ParameterDirection.Input, txtF4.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F5", SqlDbType.NVarChar, ParameterDirection.Input, txtF5.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F6", SqlDbType.NVarChar, ParameterDirection.Input, txtF6.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F7", SqlDbType.NVarChar, ParameterDirection.Input, txtF7.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F8", SqlDbType.NVarChar, ParameterDirection.Input, txtF8.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F9", SqlDbType.NVarChar, ParameterDirection.Input, txtF9.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F10", SqlDbType.NVarChar, ParameterDirection.Input, txtF10.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F11", SqlDbType.NVarChar, ParameterDirection.Input, txtF11.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F12", SqlDbType.NVarChar, ParameterDirection.Input, txtF12.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F13", SqlDbType.NVarChar, ParameterDirection.Input, txtF13.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F14", SqlDbType.NVarChar, ParameterDirection.Input, txtF14.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F15", SqlDbType.NVarChar, ParameterDirection.Input, txtF15.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, Session["userid"]));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, sID));

                    SqlHelper.ExecuteNonQuery(CommandType.Text, sSQL, lstParameter);
                    MyTrans.Commit();
                    grvData3.EditIndex = -1;
                    BindDataToGrid();
                    MyCommon.AddMessage(this.Master, "Cập nhật thành công", MyConst.MessageType.Success);
                }
                catch (Exception)
                {
                    MyTrans.Rollback();
                }
            }
            else if (e.CommandName == AIC_KQKT.MyConst.Action.Addnew)
            {
                SqlTransaction MyTrans = SqlHelper.BeginTransaction();
                try
                {
                    TextBox txtF1New = (TextBox)grvData3.FooterRow.FindControl("txtF1New");
                    TextBox txtF2New = (TextBox)grvData3.FooterRow.FindControl("txtF2New");
                    TextBox txtF3New = (TextBox)grvData3.FooterRow.FindControl("txtF3New");
                    TextBox txtF4New = (TextBox)grvData3.FooterRow.FindControl("txtF4New");
                    TextBox txtF5New = (TextBox)grvData3.FooterRow.FindControl("txtF5New");
                    TextBox txtF6New = (TextBox)grvData3.FooterRow.FindControl("txtF6New");
                    TextBox txtF7New = (TextBox)grvData3.FooterRow.FindControl("txtF7New");
                    TextBox txtF8New = (TextBox)grvData3.FooterRow.FindControl("txtF8New");
                    TextBox txtF9New = (TextBox)grvData3.FooterRow.FindControl("txtF9New");
                    TextBox txtF10New = (TextBox)grvData3.FooterRow.FindControl("txtF10New");
                    TextBox txtF11New = (TextBox)grvData3.FooterRow.FindControl("txtF11New");
                    TextBox txtF12New = (TextBox)grvData3.FooterRow.FindControl("txtF12New");
                    TextBox txtF13New = (TextBox)grvData3.FooterRow.FindControl("txtF13New");
                    TextBox txtF14New = (TextBox)grvData3.FooterRow.FindControl("txtF14New");
                    TextBox txtF15New = (TextBox)grvData3.FooterRow.FindControl("txtF15New");

                    string sql = "INSERT INTO dbo.tblBaoCaoMau10_B( KyBaoCaoID_DM ,DonViID ,F1 ,F2 ,F3 ,F4 ,F5 ,F6 ,F7 ,F8 ,F9 ,F10 ,F11 ,F12 ,F13 ,F14 ,F15 ,CreateDate ,UpdateDate ,UserID) " +
                                 "VALUES  ( @KyBaoCaoID_DM,@DonViID ,@F1,@F2,@F3,@F4,@F5,@F6,@F7,@F8,@F9,@F10,@F11,@F12,@F13,@F14,@F15,GETDATE(),GETDATE(),@UserID)";
                    List<SqlParameter> lstParameter = new List<SqlParameter>();
                    lstParameter.Add(SqlHelper.CreateParameterObject("@KyBaoCaoID_DM", SqlDbType.Int, ParameterDirection.Input, Convert.ToInt32(cboKyBaoCao.SelectedValue)));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, MyCommon.GetDonViID(Session["userid"].ToString())));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F1", SqlDbType.NVarChar, ParameterDirection.Input, txtF1New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F2", SqlDbType.NVarChar, ParameterDirection.Input, txtF2New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F3", SqlDbType.NVarChar, ParameterDirection.Input, txtF3New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F4", SqlDbType.NVarChar, ParameterDirection.Input, txtF4New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F5", SqlDbType.NVarChar, ParameterDirection.Input, txtF5New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F6", SqlDbType.NVarChar, ParameterDirection.Input, txtF6New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F7", SqlDbType.NVarChar, ParameterDirection.Input, txtF7New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F8", SqlDbType.NVarChar, ParameterDirection.Input, txtF8New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F9", SqlDbType.NVarChar, ParameterDirection.Input, txtF9New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F10", SqlDbType.NVarChar, ParameterDirection.Input, txtF10New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F11", SqlDbType.NVarChar, ParameterDirection.Input, txtF11New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F12", SqlDbType.NVarChar, ParameterDirection.Input, txtF12New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F13", SqlDbType.NVarChar, ParameterDirection.Input, txtF13New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F14", SqlDbType.NVarChar, ParameterDirection.Input, txtF14New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F15", SqlDbType.NVarChar, ParameterDirection.Input, txtF15New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, Convert.ToInt32(Session["userid"].ToString())));

                    int Result = SqlHelper.ExecuteNonQuery(CommandType.Text, sql, lstParameter);
                    if (Result == 0)
                    {
                        MyCommon.AddMessage(this.Master, "Có lỗi xảy ra trong quá trình thêm mới", MyConst.MessageType.Error);
                    }
                    else
                    {
                        MyCommon.AddMessage(this.Master, "Thêm mới thành công !", MyConst.MessageType.Success);
                    }

                    MyTrans.Commit();
                    BindDataToGrid();
                }
                catch (Exception)
                {
                    MyCommon.AddMessage(this.Master, "Có lỗi xảy ra trong quá trình thêm mới", MyConst.MessageType.Error);
                    MyTrans.Rollback();
                }
            }
            else if ((sCommandName == AIC_KQKT.MyConst.Action.Delete))
            {
                string sql = "DELETE FROM dbo.tblBaoCaoMau10_B WHERE ID = @ID";
                List<SqlParameter> lstParameter = new List<SqlParameter>();

                lstParameter.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, sID));
                SqlHelper.ExecuteNonQuery(CommandType.Text, sql, lstParameter);

                grvData3.EditIndex = -1;
                BindDataToGrid();
                MyCommon.AddMessage(this.Master, "Xóa thành công !", MyConst.MessageType.Success);
            }
        }

        protected void grvData3_RowEditing(object sender, GridViewEditEventArgs e)
        {

            grvData3.EditIndex = e.NewEditIndex;
            BindDataToGrid3();
        }

        protected void grvData3_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grvData3.EditIndex = -1;
            BindDataToGrid3();
        }

        private void BindDataToGrid3()
        {
            bool IsEmpty = false;
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@KyBaoCaoID_DM", SqlDbType.Int, ParameterDirection.Input, cboKyBaoCao.SelectedValue));
            arrParams.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, MyCommon.GetDonViID(Session["userid"].ToString())));

            string sql = "SELECT * FROM dbo.tblBaoCaoMau10_B WHERE DonViID = @DonViID AND KyBaoCaoID_DM = @KyBaoCaoID_DM";
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql, arrParams);
            if (dt.Rows.Count == 0)
            {
                IsEmpty = true;
                DataRow dr = dt.NewRow();
                dr["F1"] = "";
                dr["F2"] = "";
                dr["F3"] = "";
                dr["F4"] = "";
                dr["F5"] = "";
                dr["F6"] = "";
                dr["F7"] = "";
                dr["F8"] = "";
                dr["F9"] = "";
                dr["F10"] = "";
                dr["F11"] = "";
                dr["F12"] = "";
                dr["F13"] = "";
                dr["F14"] = "";
                dr["F15"] = "";
                dt.Rows.InsertAt(dr, 0);

            }
            grvData3.ShowFooter = true;
            grvData3.DataSource = dt;
            grvData3.DataBind();
            if (IsEmpty)
            {
                grvData3.Rows[0].Visible = false;
            }
        }

        #endregion Grid3Command
        protected void imgNext_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("BaoCaoMau7_B.aspx?KyBaoCao=" + cboKyBaoCao.SelectedValue);
        }
    }
}
